package org.platform.processor.task.check.impl;

import com.mc.mic.core.tool.helper.SpringContextHelper;
import lombok.extern.slf4j.Slf4j;
import org.platform.constant.TaskConstant;
import org.platform.dao.mapper.FdStepInfoMapper;
import org.platform.mo.bussmo.task.CheckItemResult;
import org.platform.mo.db.FdStepInfo;
import org.platform.processor.task.check.ICheckItem;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.util.List;

/**
 * 该类主要判断: 任务各个关键步骤是否正常执行
 * 检查步骤: 根据任务id数据库查询,逐一判断执行的状态,如果全部正常,则返回成功
 *
 * @author FengJie
 * @date 2018/6/25
 */
@Slf4j
public class CrucialStepCheckItemImpl implements ICheckItem {
    private FdStepInfoMapper stepInfoMapper;

    /**
     * 手动注入 Mapper
     */
    public CrucialStepCheckItemImpl() {
        this.stepInfoMapper = SpringContextHelper.getBean(FdStepInfoMapper.class);
    }

    @Override
    public CheckItemResult getCheckResult(String taskSerialNumber, String jobTaskID, File file) {
        CheckItemResult result = new CheckItemResult(true, "正常");
        Example example = new Example(FdStepInfo.class);
        example.createCriteria().andEqualTo("taskProcessId", taskSerialNumber + "_" + jobTaskID);
        List<FdStepInfo> stepInfoList = stepInfoMapper.selectByExample(example);
        for (FdStepInfo step : stepInfoList) {
            //  如果不是正常执行,执行结果就是false
            if (!TaskConstant.TaskStepStatusEnum.NORMAL.getValue().equals(step.getStepStatus())) {
                result.setStatus(false);
                result.setConclusion(step.getStepName() + TaskConstant.TaskStepStatusEnum.NORMAL.getName());
                break;
            }
        }
        return result;
    }

}
